/**
 * Created by shandianhaizan on 16/6/15.
 */




var AV = require('leanengine');

var model = {};

model.name = 'Production';
model.getQuery = function(){
    return new AV.Query(model.name);
};


model.formatQuery = function(arg_query, arg_params) {
    if(arg_params.name)  {
        arg_query.contains('name', arg_params.name);
        var codeQuery = new AV.Query(model.name);
        codeQuery.contains('code', arg_params.name);
        arg_query = AV.Query.or(arg_query, codeQuery);
    }
    if(arg_params.tag) {
        arg_query.equalTo('tags', arg_params.tag);
    }
    if(arg_params.summary) {
        arg_query.contains('summary', arg_params.summary);
    }

    if(arg_params.state) {
        if(arg_params.state!=2){
            arg_query.equalTo("state", arg_params.state);
        }else{
            arg_query.equalTo("stock", 0); //查找售罄的产品
        }
    }
    return arg_query;
};


model.getObject = AV.Object.extend(model.name);





//Todo check param valid
model.validation  = function(params){
  if(params.saleTime) params.saleTime = new Date(params.saleTime);
  return {success:true};
};





module.exports = model;







